package product.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.feign.utils.R;
import com.feign.valid.AddGroup;
import com.feign.valid.UpdateGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import product.domain.Attr;
import product.domain.AttrGroup;
import product.service.AttrAttrgroupRelationService;
import product.service.AttrGroupService;
import org.springframework.web.bind.annotation.*;
import product.service.AttrService;
import product.utils.GetUtils;
import product.vo.AttrGroupWithAttrsVo;
import product.vo.AttrRelationVo;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 属性分组(AttrGroup)表控制层
 *
 * @author panghu
 * @since 2023-03-04 21:09:17
 */
@RestController
@RequestMapping("/product/attrGroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private GetUtils getUtils;

    @Autowired
    private AttrService attrService;

    @Autowired
    private AttrAttrgroupRelationService relationService;

    @GetMapping("list/{catId}")
    public R getAttrGroupPage(HttpServletRequest request, @PathVariable Long catId) {
        Map<String, Object> params = getUtils.getParamsWithMapByRequest(request);
        Page<AttrGroup> attrGroupPage = attrGroupService.queryPage(params, catId);
        return R.ok().put("data", attrGroupPage);
    }

    /**
     * 删除属性分组
     *
     * @param ids
     * @return
     */
    @PostMapping("remove")
    public R removeCategory(@RequestBody Long[] ids) {
        boolean result = attrGroupService.removeBatchByIds(List.of(ids));
        return result ? R.ok("删除属性分组成功") : R.error("删除属性分组失败");
    }


    /**
     * 增加属性分组
     *
     * @param attrGroup
     * @return
     */
    @PostMapping("save")
    public R save(@Validated({AddGroup.class}) @RequestBody AttrGroup attrGroup) {
        boolean result = attrGroupService.save(attrGroup);
        return result ? R.ok("增加属性分组成功") : R.error("增加属性分组失败");
    }

    /**
     * 修改属性分组
     *
     * @param attrGroup
     * @return
     */
    @PutMapping("update")
    public R edit(@Validated({UpdateGroup.class}) @RequestBody AttrGroup attrGroup) {
        boolean result = attrGroupService.updateById(attrGroup);
        return result ? R.ok("修改属性分组成功") : R.error("修改属性分组失败");
    }


    /**
     * 获取单个属性分组的数据
     *
     * @param attrGroupId
     * @return
     */
    @GetMapping("info/{attrGroupId}")
    public R info(@PathVariable Long attrGroupId) {
        AttrGroup attrGroup = attrGroupService.getAttrGroupById(attrGroupId);
        return R.ok("查询成功").put("data", attrGroup);
    }

    /**
     * 查询属性分组关联的基础属性列表
     *
     * @param attrGroupId
     * @return
     */
    @GetMapping("{attrGroupId}/attr/relation")
    public R findAttrRelation(@PathVariable Long attrGroupId) {
        List<Attr> baseAttrList = attrGroupService.getAttrRelation(attrGroupId);
        return R.ok().put("data", baseAttrList);
    }

    @PostMapping("attr/relation/delete")
    public R removeAttrRelation(@RequestBody AttrRelationVo[] attrRelationVos) {
        boolean result = attrGroupService.removeAttrRelation(attrRelationVos);
        return R.ok("移除成功");
    }

    @GetMapping("{attrGroupId}/noAttr/relation")
    public R attrNoRelation(@PathVariable("attrGroupId") Long attrGroupId, HttpServletRequest request) {
        Map<String, Object> params = getUtils.getParamsWithMapByRequest(request);
        Page<Attr> attrPage = attrService.getNoRelationAttr(params, attrGroupId);
        return R.ok().put("data", attrPage);
    }

    /**
     * 新增关联
     *
     * @param attrRelationVoList
     * @return
     */
    @PostMapping("attr/relation")
    public R addRelation(@RequestBody List<AttrRelationVo> attrRelationVoList) {
        relationService.saveBatchRelation(attrRelationVoList);
        return R.ok();
    }

    /**
     * 获取分类下所有分组和关联的属性
     *
     * @param catId
     * @return
     */
    @GetMapping("{catId}/withAttr")
    public R getWithAttr(@PathVariable Long catId) {
        List<AttrGroupWithAttrsVo> list = attrGroupService.getAttrGroupWithAttrsByCatId(catId);
        return R.ok().put("data", list);
    }
}

